<?PHP
class odal_Pager{
	var $m_currentPageNo;
	var $m_sortCriteria;
	var $m_sortDirection = "asc";
	var $m_sortType = "string";
	var $m_pagesize = 10;
	var $m_aResult = Array();
	var $m_aCurrentPage = Array();
	var $m_bFirstPage = false;
	var $m_bLastPage = false;
	
	function odal_Pager(){
	}//odal_Pager
		
	function getColumnValueByIdAndColumnName( $p_id, $p_col ){
		$ret = null;
		if ( isset( $this->m_aResult[$p_id][$p_col] ) ){
			$ret = $this->m_aResult[$p_id][$p_col];
		}//if
		return $ret;
	}//getColumnValueById
	
	function getCommalist(){
		$commalist = null;
		if ( $this->m_aCurrentPage != null ){
			$commalist = "(" . implode( ",", $this->m_aCurrentPage ) . ")";
		}//if
		return $commalist;
	}//getCommalist

	function getCurrentPageNo(){
		return $this->m_currentPageNo;
	}//getCurrentPageNo

	function getElementIdAfterGivenId( $p_elmid ){
		$elmid = $this->_setPointer2ElmId( $p_elmid );
		$this->_setLastPage();
		next( $this->m_aResult );
		$elmid = key( $this->m_aResult );
		if ( $this->isLastPage() ){
			$this->_setLastPage();
			$elmid = null;
		}//if
		return $elmid;
	}//getElementIdAfterGivenId

	function getElementIdPreviousGivenId( $p_elmid ){
		$this->_setPointer2ElmId( $p_elmid );
		prev( $this->m_aResult );
		return key( $this->m_aResult );
	}//getElementIdAfterGivenId
	
	function getFirstElementNo(){
		return ( ( ( $this->getCurrentPageNo() - 1 ) * ( $this->getPagesize() ) ) + 1 );
	}//getFirstElementNo
	
	function getFirstPage( $debug = false ){
		reset( $this->m_aResult );
		$this->_setCurrentPageNo( 1 );
		$this->m_bFirstPage = true;
		$this->m_bLastPage = false;
		return $this->getIdsForPage( 1 );
	}//getFirstPage
	
	function getIdsForCurrentPage(){
		$this->m_aCurrentPage = Array();
		$elmId = key( $this->m_aResult );
		if ( $elmId !== null ){
			$this->m_aCurrentPage[] = $elmId;
			$i=0;
			while ( ( next( $this->m_aResult ) ) && ( $i < $this->m_pagesize - 1 ) ){
				$i++;
				$this->m_aCurrentPage[] = key( $this->m_aResult );
			}//while
		}//if
		return $this->m_aCurrentPage;
	}//getIdsForCurrentPage

	function getIdsForPage( $p_pageNo ){
		$this->setPointerOnPage( $p_pageNo );
		$aIds = $this->getIdsForCurrentPage(); 
		$this->_setPageNoAfterGetPage( $p_pageNo );
		return $aIds;
	}//getIdsForPage

	function getLastElementNo(  ){
		if ( $this->isLastPage() ){
			$no = $this->getResultCount();
		}else{
			$no = $this->getCurrentPageNo()  * $this->getPagesize();
		}//else
		return $no;
	}//getLastElementNo

	function getLastPage( $debug = false ){
		$rest = count( $this->getResult() ) / $this->getPagesize();
		if ( $rest == 0 ){
			$rest == $this->getPagesize();
		}//if 
		end( $this->m_aResult );
		for ( $i=0; $i < $rest ; $i++ ){
			prev( $this->m_aResult );
		}//for
		$this->m_bLastPage = true;
		$this->m_bFirstPage = false;
		return $this->getIdsForPage( $this->getPagecount() );
	}//getLastPage

	function getNextPage(){
		$aIds = $this->getIdsForPage( $this->getCurrentPageNo() );
		$this->_setLastPage();
		return $aIds;
	}//getNextPage

	function getPage4Id( $id ){
		$i = 1;
		$pageStartId = 0;
		$pageNo = 0;
		reset( $this->m_aResult );
		while (  ( list ($key, $val ) = each( $this->m_aResult ) ) && $key != $id ){
			if ( ( ( $i - 1 ) % $this->m_pagesize ) == 0 ){
				$pageStartId = $key;
				$pageNo++;
			}//if
			$i++;
		}//while
		if ( $pageStartId != 0 ){
			do{
				prev( $this->m_aResult );
			}while ( key( $this->m_aResult ) != $id );
		}//if
		$aIds = $this->getIdsForPage( $pageNo );
		$this->_setPageNoAfterGetPage( $pageNo );
		return $aIds;
	}//getPage4Id

	function getPageByPageNo( $p_pageNo, $debug = false  ){
		//returns commalist
		if ( $debug ){
			echo "[pager->getPageByPageNo] key( this->m_aResult ): " . key( $this->m_aResult ) . "<br>";
			//print_r( $this->m_aResult );
		}//if
		$this->getIdsForPage( $p_pageNo );
		$cl = $this->getCommalist();
		if ( $debug ){
			echo "[pager->getPageByPageNo] cl: " . $cl . "<br>";
		}//if
		$this->_setPageNoAfterGetPage( $p_pageNo );
		return $cl;
	}//getPageByPageNo
	
	function getPagecount(){
		return ceil( count( $this->m_aResult ) / $this->m_pagesize );
	}//getPagecount
	
	function getPageNo4Id( $id ){
		return ceil( $this->getPositionInResultById( $id ) / $this->m_pagesize );
	}//getPageNo4Id
	
	function getPagesize(){
		return $this->m_pagesize;		
	}//getPagesize
	
	function getPositionInPage( $p_id, $p_debug = false ){
		$retPos = 0;
		reset( $this->m_aCurrentPage );
		foreach ( $this->m_aCurrentPage as $key => $id ){
			if ( $p_id == $id ){
				$retPos = $key;
				break;
			}//if
		}//foreach
		return $retPos;
	}//getPositionInPage

	function getPositionInResultById( $p_id ) {
		$retPos = 0;
		foreach( $this->m_aResult as $id => $arr ) {
			$retPos++;
			if( $p_id == $id ) {
				break;
			}//if
		}//foreach
		
		return $retPos;
	}//getPositionInResultById

	function getPreviousPage(){
		if ( !$this->isFirstPage() ){
			for( $i=0; $i < ( 2 * $this->m_pagesize ) ; $i++ ){
				prev( $this->m_aResult );
			}//for
			$this->_setCurrentPageNo( $this->getCurrentPageNo() - 1 );
			$this->m_bFirstPage = false;
			if ( ( $this->getCurrentPageNo() == 1 ) ){
				$this->m_bFirstPage = true;
			}//if
			$this->getIdsForPage( $this->getCurrentPageNo() );
			return $this->getCommalist();
		}//if
	}//getPreviousPage
	
	function &getResult(){
		return $this->m_aResult;
	}//	getResult

	function getResultCount(){
		return count( $this->m_aResult );
	}//	getResultCount

	function getSortCriteria(){
		return $this->m_sortCriteria;		
	}//getSortCriteria
	
	function getSortDirection(){
		return $this->m_sortDirection;
	}//getSortDirection

	function getSortType(){
		return $this->m_sortType;
	}//getSorttype
	
	function isFirstPage(){
		return $this->m_bFirstPage;
	}//isFirstPage

	function isLastPage(){
		return $this->m_bLastPage;
	}//isLastPage
	
	function setColumnValueByIdAndColumnName( $p_value, $p_id, $p_col ){
		if ( isset( $this->m_aResult[$p_id] ) ){
			$this->m_aResult[$p_id][$p_col] = $p_value;
		}//if
	}//getColumnValueById

	function setPagesize( $p_pageSize ){
		$this->m_pagesize = $p_pageSize;
	}//setPagesize

	function setPointerOnPage( $p_pageNo ){
		$i = 1;
		$pageNo = 1;
		reset( $this->m_aResult );
		while ( $pageNo != $p_pageNo && ( list ( $key, $val ) = each( $this->m_aResult ) ) ){
			if ( $i > 1 && ( ( ( $i - 1 ) % $this->m_pagesize ) == 0 ) ){
				$pageNo++;
			}//if
//			echo "[pager->getPageByPageNo] key: " . $key . "<br>";
//			echo "[pager->getPageByPageNo] val: "; print_r( $val ); echo "<br>";
//			print_r( $this->m_aResult );
			$i++;
		}//while
		if ( $pageNo > 1 ){
			if ( !prev( $this->m_aResult ) ){
				end( $this->m_aResult );
			}//if;
		}//if
	}//setPointerOnPage

	function setResult( &$p_result ){
		$this->m_aResult =& $p_result;
	}//	setResult

	function setResultFromDB( &$dbconn, $resFile, $resNamespace, $aSortCols, $where = "", $debug =  false ){
		$aParams = Array( "where" => $where );
		$this->setResultFromDBWithParams( $dbconn, $resFile, $resNamespace, $aParams, $aSortCols, $debug );
	}//setResultFromDB

	function setResultFromDBWithParams( &$dbconn, $resFile, $resNamespace, $p_aParams, $p_aSortCols, $debug =  false ){
		$aSqlParams = Array();
		if ( ( $p_aParams != null ) && ( gettype( $p_aParams ) == "array" ) ){
			foreach ( $p_aParams as $key => $value ){
				$aSqlParams[ $key ] = $value;
			}//foreach
		}//if
		$i = 1;
		if ( ( $p_aSortCols != null ) && ( gettype( $p_aSortCols ) == "array" ) ){
			foreach ( $p_aSortCols as $dummy => $aCol ){
				foreach ( $aCol as $key => $value ){
					$aSqlParams[$key . $i] = $value;
				}//foreach
				$i++;
			}//foreach
		}//if
		$aSqlBindvars = Array();
		$aSqlBindvars['p_cursor'] = "__cursor";
		$this->m_aResult = $dbconn->getPKResultOnResource( $aSqlParams, $aSqlBindvars, $resFile, $resNamespace );
		$this->sortResult( $p_aSortCols[0]['sortname'] );
	}//setResultFromDBWithParams

	function sortResult( $p_criteria, $p_direction = "asc", $p_type = "string" ){
		$this->m_sortCriteria = $p_criteria;
		$this->m_sortDirection = $p_direction;
		$this->m_sortType = $p_type;

		if ( $this->m_sortDirection == "asc" ){
			if ( $this->m_sortType == "string" ){
				$compare = create_function('$a, $b','return strcasecmp($a["'. $this->m_sortCriteria .'"], $b["'. $this->m_sortCriteria .'"]);');
			}elseif( $this->m_sortType == "numeric" ){
				$compare = create_function('$a, $b','if ( $a["'. $this->m_sortCriteria .'"] == $b["'. $this->m_sortCriteria .'"]) return 0; else return ($a["'. $this->m_sortCriteria .'"] < $b["'. $this->m_sortCriteria .'"]) ? -1 : 1;');
			}//elseif
		} elseif ( $this->m_sortDirection == "desc" ){
			if ( $this->m_sortType == "string" ){
				$compare = create_function('$a,$b','return strcasecmp($b["'. $this->m_sortCriteria .'"], $a["'. $this->m_sortCriteria .'"]);');
			}elseif( $this->m_sortType == "numeric" ){
				$compare = create_function('$a, $b','if ( $a["'. $this->m_sortCriteria .'"] == $b["'. $this->m_sortCriteria .'"]) return 0; else return ($a["'. $this->m_sortCriteria .'"] > $b["'. $this->m_sortCriteria .'"]) ? -1 : 1;');
			}//elseif
		}//elseif
		uasort( $this->m_aResult, $compare );
	}//sortResult
	
	function _setCurrentPageNo( $p_pageNo ){
		$this->m_currentPageNo = $p_pageNo;
	}//_setCurrentPageNo
	
	function _setLastPage(){
		$checkLastPage = true;
		$this->m_bLastPage = false;
		if ( $this->getCurrentPageNo() < $this->getPageCount() ){
			$checkLastPage = false;
		}//if
		if ( $checkLastPage ){
			if ( !next( $this->m_aResult ) ){
				$this->m_bLastPage = true;
			}else{
				prev( $this->m_aResult );
			}//else
			if ( key( $this->m_aResult ) === null ){
				end( $this->m_aResult );
			}//if
		}//if
	}//_setLastPage

	function _setPageNoAfterGetPage( $pageNo ){
		$this->m_currentPageNo = $pageNo;
		$this->m_bFirstPage = false;
		if ( $pageNo == 1 ){
			$this->m_bFirstPage = true;
		}//if
		$this->_setLastPage();
	}//_setPageNoAfterGetPage

	function _setPointer2ElmId( $p_elmid ){
		reset( $this->m_aResult );
		$elmid = 0;
		while ( ( $elmid != $p_elmid ) && ( list ( $elmid, $val ) = each( $this->m_aResult ) ) ){
		}//while
		prev( $this->m_aResult );
		$this->_setLastPage();
		return key( $this->m_aResult );
	}//_setPointer2ElmId
	
}//odal_Pager
?>
